//
//  TableHeadView.m
//  BCW
//
//  Created by Mac on 16/3/29.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "TableHeadView.h"
#import "LotteryResultView.h"

#define NumberImgWH 30
#define NumberImgMargin 15

@interface TableHeadView()

@property (nonatomic, strong) NSArray      *numberImgArr;

@end


@implementation TableHeadView

- (NSArray *)numberImgArr{
    if (!_numberImgArr) {
        _numberImgArr = [[NSArray alloc] initWithObjects:@"z_0", @"z_1", @"z_2", @"z_3",
                         @"z_4", @"z_5", @"z_6",
                         @"z_7", @"z_8", @"z_9", nil];
    }
    return _numberImgArr;
}

- (instancetype)initWithFrame:(CGRect)frame{
    return [self initWithType:TableHeadViewThird];
}

- (instancetype)initWithType:(TableHeadViewType )type{
    CGFloat h;
    if (type == TableHeadViewFirst) {
        h = 100;
    }
    else if(type == TableHeadViewScoend){
        h = 70;
    }
    else{
        h = 35;
    }
    
    CGRect customF = CGRectMake(0, 0, BCWScreenW, h);
    if (self = [super initWithFrame:customF]) {
        if (type == TableHeadViewFirst) {
            [self setFirstView];
        }
        else if(type == TableHeadViewScoend){
            [self setScoendView];
        }
        else{
            //
        }
        
        LotteryResultView *resultView = [[LotteryResultView alloc] initWithFrame:CGRectMake(0, self.height - 35, self.width, 35)];
        resultView.tag = 2;
        [self addSubview:resultView];
    }
    return self;
}

- (void)setFirstView{
    //
    LotteryResultView *result = [[LotteryResultView alloc] initWithFrame:CGRectMake(0, 0, self.width, 25)];
    result.tag = 1;
    [self addSubview:result];
    
    
    // number
    CGFloat temp = (self.width - 195) / 2;
    CGFloat y = 30, x = temp;
    for (int i = 0; i < 5; i++) {
        x = (NumberImgWH + NumberImgMargin) * i + temp;
        UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, NumberImgWH, NumberImgWH)];
        img.tag = 10 + i;
//        img.image = [UIImage imageNamed:self.numberImgArr[i]];
        [self addSubview:img];
        
    }
    
}

- (void)setLotteryModel:(LotteryStatusResultModel *)lotteryModel{
    
    // 开奖号
    for (int i = 0; i < 5; i++) {
        NSInteger imgIndx = [[lotteryModel.open_result substringWithRange:NSMakeRange(i*2, 1)] integerValue];
        UIImageView *img = [self viewWithTag:10 + i];
        img.image = [UIImage imageNamed:self.numberImgArr[imgIndx]];
        
    }
    
    //
    NSInteger phase = [[lotteryModel.open_phase substringFromIndex:8] integerValue];
    LotteryResultView *view = [self viewWithTag:1];
    view.text = [NSString stringWithFormat:@"第%li期最新开奖:", phase];
    
    view = [self viewWithTag:2];
    
    NSString *temp = @"0";
    if (lotteryModel.next_phase) {
        temp = lotteryModel.next_phase;
    }
    
    
    view.text = [NSString stringWithFormat:@"%@ %li期 投注参考", temp, phase + 1];
    
}

- (void)setScoendView{
    UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.width, 35)];
    img.backgroundColor = RedColor;
    
    [self addSubview:img];
}

- (void)setTitle:(NSString *)title{
    LotteryResultView *view = [self viewWithTag:2];
    view.text = title;
}
@end
